using UnityEngine;

public class SmokeMovement : MonoBehaviour
{
    public Transform smokeTransform;
    public SpriteRenderer sr;
    public AnimationCurve alphaCurve;
    public AnimationCurve sizeCurve;
    public Vector3 speed = new Vector3(-1f, 2f, 0);
    public float randRotSpeedMin = 10f;
    public float randRotSpeedMax = 50f;
    private float rotSpeed;
    public float totalLifeTime = 5;
    private float time = 0;
    
    // Start is called once before the first execution of Update after the MonoBehaviour is created
    void Start()
    {
        smokeTransform.Rotate(0, 0, Random.Range(0f, 360f));
        rotSpeed = (Random.value > 0.5f ? 1 : -1) * Random.Range(randRotSpeedMin, randRotSpeedMax);
        sr.color = new Color(1, 1, 1, alphaCurve.Evaluate(0));
        transform.localScale = Vector3.one * sizeCurve.Evaluate(0);
    }

    // Update is called once per frame
    void Update()
    {
        time += Time.deltaTime;
        float p = time / totalLifeTime;
        transform.Translate(speed * Time.deltaTime);
        smokeTransform.Rotate(0, 0, rotSpeed * Time.deltaTime);
        sr.color = new Color(1, 1, 1, alphaCurve.Evaluate(p));
        smokeTransform.localScale = Vector3.one * sizeCurve.Evaluate(p);
        if (time> totalLifeTime) Destroy(gameObject);
    }
}
